<?php 
namespace Sisesporte;

use Zend\Mvc\ModuleRouteListener,
    Zend\Mvc\MvcEvent,
    Zend\ModuleManager\ModuleManager;
use Zend\Authentication\AuthenticationService,
    Zend\Authentication\Storage\Session as SessionStorage;

use Sisesporte\Service\TipoTelefoneService as TipoTelefoneService,
    Sisesporte\Service\ProfissaoService as ProfissaoService,
    Sisesporte\Service\ModalidadeService as ModalidadeService,
    Sisesporte\Service\InstituicaoEnsinoService as InstituicaoEnsinoService,
    Sisesporte\Service\HorarioFuncionamentoService as HorarioFuncionamentoService,
    Sisesporte\Service\OrgaoExpedidorService as OrgaoExpedidorService,
    Sisesporte\Service\AlunoService as AlunoService,
    Sisesporte\Service\CidadeService as CidadeService,
    Sisesporte\Service\AuthService as AuthService,
    Sisesporte\Service\ModalidadeProfessorService as ModalidadeProfessorService,
    Sisesporte\Service\TurmaService as TurmaService,
    Sisesporte\Service\ProfessorService as ProfessorService,
    Sisesporte\Service\PreMatriculaService as PreMatriculaService,
    Sisesporte\Service\FinanceiroService as FinanceiroService,
    Sisesporte\Service\VagasModalidadeService as VagasModalidadeService,
    Sisesporte\Service\TurmaAnoService as TurmaAnoService,
    Sisesporte\Service\VagasTurmaService as VagasTurmaService;

use SisesporteAdmin\Form\AlunoForm as AlunoForm;
use Sisesporte\Auth\Adapter as AuthAdapter;

use Sisesporte\View\Helper\AbsoluteUrl;

class Module {

    public function onBootstrap(MvcEvent $e) {
        $e->getApplication()
                ->getEventManager()
                ->getSharedManager()
                ->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
                            $controller = $e->getTarget();
                            $controllerClass = get_class($controller);
                            $moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
                            $config = $e->getApplication()->getServiceManager()->get('config');
                            if (isset($config['module_layouts'][$moduleNamespace])) {
                                $controller->layout($config['module_layouts'][$moduleNamespace]);
                            }
                        }, 98);

//        $config = $e->getApplication()->getServiceManager()->get('config');
//        $phpSettings = $config['phpSettings'];
//        if ($phpSettings) {
//            foreach ($phpSettings as $key => $value) {
//                ini_set($key, $value);
//            }
//        }
    }

    /**
     * Retorna Arquivo de Configuração do Módulo
     * @return Arquivo de Configuração
     */
    public function getConfig() {
        return include __DIR__ . '/config/module.config.php';
    }

    /**
     * Retorna os Namespaces
     * @return array de NamesSpaces
     */
    public function getAutoloaderConfig() {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ . 'Admin' => __DIR__ . '/src/' . __NAMESPACE__ . "Admin",
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

    /**
     * Verifica Se o Usuário esta logado
     * @param \Sisesporte\ModuleManager $moduleManager
     * @return type
     */
    public function init(ModuleManager $moduleManager) {
        $sharedEvents = $moduleManager->getEventManager()->getSharedManager();
        $sharedEvents->attach("SisesporteAdmin", 'dispatch', function($e) {
                    $auth = new AuthenticationService();
                    $auth->setStorage(new SessionStorage("SisesporteAdmin"));

                    $controller = $e->getTarget();
                    $matchedRoute = $controller->getEvent()->getRouteMatch()->getMatchedRouteName();

                    if (!$auth->hasIdentity() and ($matchedRoute == "sisesporte-admin" or $matchedRoute == "sisesporte-admin-interna")) {
                        return $controller->redirect()->toRoute('sisesporte-admin-auth');
                    }
                }, 99);
    }

    /**
     * Registra Servicos
     * @return array Service
     */
    public function getServiceConfig() {
        return array(
            'factories' => array(
                'Sisesporte\Service\OrgaoExpedidorService' => function($service) {
                    return new OrgaoExpedidorService($service->get('Doctrine\ORM\EntityManager'));
                },
                'Sisesporte\Service\TipoTelefoneService' => function($service) {
                    return new TipoTelefoneService($service->get('Doctrine\ORM\EntityManager'));
                },
                'Sisesporte\Service\ModalidadeService' => function($service) {
                    return new ModalidadeService($service->get('Doctrine\ORM\EntityManager'));
                },
                'Sisesporte\Service\ProfissaoService' => function($service) {
                    return new ProfissaoService($service->get('Doctrine\ORM\EntityManager'));
                },
                'Sisesporte\Service\InstituicaoEnsinoService' => function($service) {
                    return new InstituicaoEnsinoService($service->get('Doctrine\ORM\EntityManager'));
                },
                'Sisesporte\Service\HorarioFuncionamentoService' => function($service) {
                    return new HorarioFuncionamentoService($service->get('Doctrine\ORM\EntityManager'));
                },
                'Sisesporte\Service\AlunoService' => function($service) {
                    return new AlunoService($service->get('Doctrine\ORM\EntityManager'));
                },
                'Sisesporte\Service\CidadeService' => function($service) {
                    return new CidadeService($service->get('Doctrine\ORM\EntityManager'));
                },
                'SisesporteAdmin\Form\AlunoForm' => function($service) {
                    return new AlunoForm(null, $service->get('Doctrine\ORM\EntityManager'));
                },
                'Sisesporte\Auth\Adapter' => function($service) {
                    return new AuthAdapter($service->get('Doctrine\ORM\EntityManager'));
                },
                'Sisesporte\Service\AuthService' => function($service) {
                    return new AuthService($service->get('Doctrine\ORM\EntityManager'));
                },
                'Sisesporte\Service\ModalidadeProfessorService' => function($service) {
                    return new ModalidadeProfessorService($service->get('Doctrine\ORM\EntityManager'));
                },
                'Sisesporte\Service\TurmaService' => function($service) {
                    return new TurmaService($service->get('Doctrine\ORM\EntityManager'));
                },
                'Sisesporte\Service\ProfessorService' => function($service) {
                    return new ProfessorService($service->get('Doctrine\ORM\EntityManager'));
                },
                'Sisesporte\Service\PreMatriculaService' => function($service) {
                    return new PreMatriculaService($service->get('Doctrine\ORM\EntityManager'));
                },
                'Sisesporte\Service\FinanceiroService' => function($service) {
                    return new FinanceiroService($service->get('Doctrine\ORM\EntityManager'));
                },
                'Sisesporte\Service\VagasModalidadeService' => function($service) {
                    return new VagasModalidadeService($service->get('Doctrine\ORM\EntityManager'));
                },                        
                'Sisesporte\Service\TurmaAnoService' => function($service) {
                    return new TurmaAnoService($service->get('Doctrine\ORM\EntityManager'));
                },                            
                'Sisesporte\Service\VagasTurmaService' => function($service) {
                    return new VagasTurmaService($service->get('Doctrine\ORM\EntityManager'));
                },                        
            ),
        );
    }

    /**
     * Registra as ViewHelpers
     * @return array ViewHelpers
     */
    public function getViewHelperConfig() {
        return array(
            'invokables' => array(
                'UserIdentity' => new View\Helper\UserIdentity(),
                'MontaMenu' => new View\Helper\MontaMenu(),
//                'FlashMessenger' => new View\Helper\FlashMessenger(),
            ),
            'factories' => array(
                // the array key here is the name you will call the view helper by in your view scripts
                'absoluteUrl' => function($sm) {
                    $locator = $sm->getServiceLocator(); // $sm is the view helper manager, so we need to fetch the main service manager
                    return new AbsoluteUrl($locator->get('Request'));
                },
            ),
        );
    }

}
